package com.st.buy.item.controller;

import com.st.buy.pojo.TbItem;
import com.st.buy.pojo.TbItemDesc;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张双虎
 */
@Controller
public class ItemController {

    @Value("${managerService}")
    private String managerService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @RequestMapping("/item/genAll")
    @ResponseBody
    public String genAll() throws IOException, TemplateException {
        //查询所有商品进行静态处理
        List<Map> items = restTemplate.getForObject("http://" + managerService + "/queryAll", List.class);
        for (Map item:items){
            gen(item);
        }
        return "OK";
    }

    @RequestMapping("/item/{itemId}")
    public String detail(@PathVariable long itemId, Model model){
        //远程调用
        //查询商品信息
        TbItem item = restTemplate.getForObject("http://" + managerService + "/queryById?itemId=" + itemId, TbItem.class);
        //处理商品的图片
        String [] images = null;
        if (StringUtils.isNoneBlank(item.getImage())){
            images = item.getImage().split(";");
        }
        //设置图片的列表
        model.addAttribute("images",images);
        //修改item的image属性，只留第一张图片
        if(images!=null && images.length>0) {
            item.setImage(images[0]);
        }
        model.addAttribute("item",item);
        //查询商品详情
        TbItemDesc itemDesc = restTemplate.getForObject("http://" + managerService + "/queryDescById?itemId=" + itemId, TbItemDesc.class);
        model.addAttribute("itemDesc",itemDesc);
        return "item";
    }

    private void gen(Map item) throws IOException, TemplateException {
        //准备数据
        Map data = new HashMap();
        //处理图片
        String [] images = null;
        if (StringUtils.isNoneBlank(item.get("image").toString())){
            images = item.get("image").toString().split(";");
        }
        //设置图片的列表
        data.put("images",images);
        //修改item的image属性，只留第一张图片
        if(images!=null && images.length>0) {
            item.put("image",images[0]);
        }
        //商品数据
        data.put("item",item);
        //商品详情数据
        data.put("itemDesc",restTemplate.getForObject("http://" + managerService + "/queryDescById?itemId=" + item.get("id"), TbItemDesc.class));
        //准备一个输出流
        //准备生成的页面存储的文件夹  这个路径可以配置
        String itemsPath = "D:\\jerry\\egobuy\\items\\";
        Writer writer = new FileWriter(itemsPath+item.get("id")+".html");
        //得到模板
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate("item.ftlh");
        //执行模板
        template.process(data,writer);
        writer.close();
        writer = null;
    }

}
